package com.support.android.designlibdemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

/**
 * Created by tukela on 2015/11/26.
 */
public class WelcomeActivity  extends Activity{
    private int pageCount;
    private ImageView[] dots;
    private int currentIndex;
    private ArrayList<View> views;
    private ViewPager viewPager_guide;
    private WelcomePagerAdapter guidePagerAdapter;
    private LinearLayout linear_guide_dots;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wel);
        initData();
        initLayout();
    }

    /**
     * 初始化布局
     */
    private void initLayout() {
		/* viewPager设置 */
        viewPager_guide = (ViewPager) findViewById(R.id.viewPager_guide);
        LayoutInflater inflater = LayoutInflater.from(this);
        Class<R.drawable> cls = R.drawable.class;// 准备反射R.drawable下资源
        for (int i = 0; i < pageCount; i++) {
            View view = inflater.inflate(R.layout.welcome_content, null);
            LinearLayout linear_guide_showImg = (LinearLayout) view
                    .findViewById(R.id.linear_guide_showImg);
            int imageId = 0;
            try {
                imageId = cls.getDeclaredField("slide" + (i + 1)).getInt(
                        R.drawable.slide1);
            } catch (Exception e) {
                imageId = R.drawable.slide1;
                e.printStackTrace();
            }
            linear_guide_showImg.setBackgroundResource(imageId);
            views.add(view);
        }
        guidePagerAdapter = new WelcomePagerAdapter(views, this);
        viewPager_guide.setAdapter(guidePagerAdapter);
        viewPager_guide.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                dots[arg0].setEnabled(false);
                dots[currentIndex].setEnabled(true);
                currentIndex = arg0;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
		/* 引导小点设置 */
        linear_guide_dots = (LinearLayout) findViewById(R.id.linear_guide_dots);
        for (int i = 0; i < pageCount; i++) {// 循环取得小点图片
            dots[i] = (ImageView) linear_guide_dots.getChildAt(i);
            if (i == 0) {
                dots[i].setEnabled(false);// 设置为白色，即选中状态
            } else {
                dots[i].setEnabled(true);// 都设为灰色
            }
        }
    }



    /**
     * 初始化数据
     */
    private void initData() {
		/* 设置标识，表示不再启动这个引导页 */

        pageCount = 3;
        dots = new ImageView[pageCount];
        currentIndex = 0;
		/* 设置viewpager数据源 */
        views = new ArrayList<View>();
    }
}
